﻿using System;
using System.Linq;

using Microsoft.WowAddonStudio.Lua.AST;
using Microsoft.WowAddonStudio.Lua.Model;
using Microsoft.WowAddonStudio.Lua.Support;

using Variable=Microsoft.WowAddonStudio.Lua.Model.Variable;

namespace Microsoft.WowAddonStudio.Lua.Adapters
{
    /// <summary>
    /// Adds documentation for the <see cref="LocalDeclaration"/> nodes in the AST.
    /// </summary>
    public class LocalDeclarationDocumentationAdapter : DocumentationAdapter<LocalDeclaration>
    {
        /// <summary>
        /// Adds the documentation for the local declaration.
        /// </summary>
        /// <param name="documentationScope">The documentation scope.</param>
        /// <param name="node">The function declaration.</param>
        public override void AddDocumentation(DocumentationScope documentationScope, LocalDeclaration node)
        {
            base.AddDocumentation(documentationScope, node);

            // Create local declarations
            var localDeclarations = node.GetIdentifiers().Select(identifier => DeclarationFactory.CreateDeclaration<Variable>(identifier, true)).ToArray();

            // The location of the declarations should be the same (and include the 'local' keyword)
            foreach (var declaration in localDeclarations)
                declaration.Location = node.Location;

            // Add local declarations to the current scope
            documentationScope.AddRange(localDeclarations);
        }
    }
}